<script setup>
document.title = "3D资源分享站 - 注册";
import {ref} from "vue";
import {userApi} from "@/api/modelApi";
import {ElMessage} from "element-plus";

const run = ref(true)
const registerFormRef = ref(null);
const uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
  code: '',
  qq: ''
});
const registerRules = ref({
  username: [
    { pattern: uPattern,message: '用户名只能包含字母、数字、下划线，长度4-16位',required: true, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '登录密码不能为空', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '授权码不能为空，请联系管理员获取！', trigger: 'blur' }
  ],
  qq: [
    { required: true, message: 'QQ不能为空', trigger: 'blur' }
  ]
})

const onSubmit = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid) =>{
    if(valid){
      if (registerForm.value.password !== registerForm.value.confirmPassword){
        ElMessage.error('两次密码输入不一致!');
        return;
      }
      try {
        const res = await userApi.register(
          registerForm.value.username,
          registerForm.value.password,
          registerForm.value.code,
          registerForm.value.qq
        )
        if (res.data.code === 200) {
          ElMessage.success('注册成功!')
          location.href = '/login'
        } else {
          // 显示后端返回的具体错误信息
          const errorMessage = res.data.msg || '注册失败，请检查输入信息！'
          ElMessage.error(errorMessage)
        }
      }catch (e){
        // 处理网络错误或其他异常
        let errorMessage = '注册失败，请稍后重试！'
        if (e.response && e.response.data && e.response.data.msg) {
          errorMessage = e.response.data.msg
        } else if (e.message) {
          errorMessage = `网络错误: ${e.message}`
        }
        ElMessage.error(errorMessage)
      }
    }else{
      ElMessage.error('请填写完整的注册信息！');
    }
  })
}

const goLogin = () => {
  run.value = !run.value
  setTimeout(()=>{
    location.href = '/login'
  },100)
}
</script>

<template>
  <div class="register-container">
    <!-- 动态背景 -->
    <div class="background-animation">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
      </div>
    </div>
    
    <transition name="fade" appear>
      <div v-if="run" class="regBox">
        <div class="register-card">
          <div class="card-header">
            <div class="logo-container">
              <div class="logo-icon">🎮</div>
              <h1 class="title">3D资源分享站</h1>
              <p class="subtitle">创建您的账户</p>
            </div>
          </div>
          
          <el-form
              ref="registerFormRef"
              :model="registerForm"
              :rules="registerRules"
              class="register-form"
              auto-complete="on"
              label-position="top"
          >
            <el-form-item label="创建账号" prop="username">
              <el-input
                  v-model="registerForm.username"
                  name="username"
                  type="text"
                  auto-complete="on"
                  placeholder="请输入您的账号"
                  size="large"
              >
                <template #prefix>
                  <span class="input-icon">👤</span>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item label="创建密码" prop="password">
              <el-input
                  v-model="registerForm.password"
                  name="password"
                  type="password"
                  auto-complete="on"
                  placeholder="请输入您的密码"
                  size="large"
                  show-password
              >
                <template #prefix>
                  <span class="input-icon">🔒</span>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input 
                v-model="registerForm.confirmPassword" 
                name="confirmPassword" 
                type="password" 
                auto-complete="on" 
                placeholder="请再次输入密码"
                size="large"
                show-password
              >
                <template #prefix>
                  <span class="input-icon">🔐</span>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item label="群授权码" prop="code">
              <el-input 
                v-model="registerForm.code" 
                name="code" 
                type="text" 
                auto-complete="on" 
                placeholder="请输入授权码"
                size="large"
              >
                <template #prefix>
                  <span class="input-icon">🎫</span>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item label="绑定QQ" prop="qq">
              <el-input 
                v-model="registerForm.qq" 
                name="qq" 
                type="text" 
                auto-complete="on" 
                placeholder="请输入QQ号"
                size="large"
              >
                <template #prefix>
                  <span class="input-icon">💬</span>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item class="buttons">
              <el-button 
                type="primary" 
                @click="onSubmit(registerFormRef)"
                size="large"
                class="register-btn"
              >
                <span class="btn-icon">🚀</span>
                立即注册
              </el-button>
              <el-button 
                @click="goLogin"
                size="large"
                class="login-btn"
              >
                <span class="btn-icon">🔙</span>
                返回登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </transition>
    
    <div class="footer">
      <div class="footer-content">
        <p class="copyright">桃丽丝研究所 · 版权所有 © 2023-2024 All Rights Reserved.</p>
        <p class="developer">开发: 雨梦妍Marissa &emsp; 2024.05.20</p>
        <div class="links">
          <a target="_blank" href="https://mtw.so/5zkafw">B站空间</a>
          <span class="separator">|</span>
          <a target="_blank" href="https://mtw.so/5GQnkd">赞助支持</a>
          <span class="separator">|</span>
          <a target="_blank" href="https://mtw.so/5yzxK3">加入我们</a>
          <span class="separator">|</span>
          <a target="_blank" href="https://mtw.so/6wJgWU">联系作者</a>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: var(--gradient-dark);
  padding-bottom: 120px; /* 为footer留出空间 */
}

.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.floating-shapes {
  position: relative;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  background: linear-gradient(45deg, var(--accent-gold), var(--accent-silver));
  border-radius: 50%;
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  top: 80%;
  left: 20%;
  animation-delay: 4s;
}

.shape-4 {
  width: 100px;
  height: 100px;
  top: 10%;
  right: 30%;
  animation-delay: 1s;
}

.shape-5 {
  width: 40px;
  height: 40px;
  top: 40%;
  left: 60%;
  animation-delay: 3s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.regBox {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 500px;
  padding: 20px;
  margin-bottom: 20px; /* 增加底部间距 */
}

.register-card {
  background: rgba(26, 26, 26, 0.9);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(212, 175, 55, 0.3);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: fadeInUp 0.8s ease-out;
}

.card-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.logo-icon {
  font-size: 48px;
  animation: pulse 2s ease-in-out infinite;
}

.title {
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-gold), var(--white));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
}

.subtitle {
  font-size: 16px;
  color: var(--accent-silver);
  margin: 0;
  font-weight: 300;
}

.register-form {
  margin-top: 30px;
}

.register-form .el-form-item {
  margin-bottom: 25px;
}

.input-icon {
  font-size: 16px;
  margin-right: 8px;
}

.buttons {
  margin-top: 40px !important;
  display: flex;
  flex-direction: row;
  gap: 15px;
}

.register-btn, .login-btn {
  flex: 1;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.3s ease;
  border: none !important;
}

.register-btn {
  background: linear-gradient(135deg, var(--accent-gold), #b8941f) !important;
  color: var(--primary-black) !important;
  box-shadow: 0 4px 15px rgba(212, 175, 55, 0.3);
}

.register-btn:hover {
  background: linear-gradient(135deg, #b8941f, var(--accent-gold)) !important;
  transform: translateY(-3px);
  box-shadow: 0 10px 30px rgba(212, 175, 55, 0.4);
}

.login-btn {
  background: linear-gradient(135deg, var(--accent-gray), var(--light-gray)) !important;
  color: var(--white) !important;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.login-btn:hover {
  background: linear-gradient(135deg, var(--light-gray), var(--accent-gray)) !important;
  transform: translateY(-3px);
  box-shadow: 0 10px 30px rgba(212, 175, 55, 0.3);
  color: var(--accent-gold) !important;
}

.btn-icon {
  font-size: 18px;
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 3;
  background: rgba(10, 10, 10, 0.8);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(212, 175, 55, 0.2);
  max-height: 120px; /* 限制footer高度 */
  overflow: hidden;
}

.footer-content {
  padding: 20px;
  text-align: center;
}

.copyright, .developer {
  color: var(--accent-silver);
  font-size: 14px;
  margin: 5px 0;
}

.links {
  margin-top: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  flex-wrap: wrap;
}

.links a {
  color: var(--accent-silver);
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
  padding: 5px 10px;
  border-radius: 6px;
}

.links a:hover {
  color: var(--accent-gold);
  background: rgba(212, 175, 55, 0.1);
  transform: translateY(-2px);
}

.separator {
  color: var(--accent-gray);
  font-size: 12px;
}

.fade-enter-active {
  animation: fadeInUp 0.8s ease-out;
}

.fade-leave-active {
  animation: fadeOutRightBig 0.5s ease-in;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .regBox {
    padding: 10px;
  }
  
  .register-card {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .buttons {
    flex-direction: column;
  }
  
  .register-btn, .login-btn {
    width: 100%;
  }
  
  .links {
    flex-direction: column;
    gap: 10px;
  }
  
  .separator {
    display: none;
  }
}
</style>
